'use strict';
import Base from './base.js';
export default class extends Base {
  /*
    **通用方法写在此处
    **2017.3.13
    **黄
    */

  // 获取部门、职位和职位对应职称
  async getdplistAction() {
    if (this.isPost) {
      const zwid = this.post('id');
      const zcpid = await this.model('or_department').where({id: this.shop_userinfo.departmentid}).getField('pid');
      const zclist = await this.model('or_position').where({floor: 2, positionid: zwid, departmentid: zcpid[0]}).select();
      if (think.isEmpty(zclist)) {
        return this.fail({info: '无职称'});
      } else {
        return this.json(zclist);
      }
    } else {
      const pid = await this.model('or_department').where({id: this.shop_userinfo.departmentid}).getField('pid', true);
      const plist = await this.model('or_position').where({floor: 1, departmentid: pid}).select();
      const data = {
        'plist': plist
      };
      return this.json(data);
    }
  }

  // 获取七牛上传图片的验证token
  async gettokenAction() {
    const qiniu = require('qiniu');

    // 需要填写你的 Access Key 和 Secret Key
    qiniu.conf.ACCESS_KEY = this.config('qiniu.ACCESS_KEY');
    qiniu.conf.SECRET_KEY = this.config('qiniu.SECRET_KEY');
    // 要上传的空间
    const bucket = this.config('qiniu.BUCKET');

    // 构建上传策略函数
    function uptoken(bucket, key) {
      const putPolicy = new qiniu.rs.PutPolicy(bucket);
      return putPolicy.token();
    }
    // 生成上传 Token
    const token = uptoken(bucket);
    const data = {'uptoken': token};
    return this.json(data);
  }

  // 获取左侧的分类列表
  async getMenuAction() {
    const mlist = await this.model('menu').where({status: 1, is_hidden: 0}).select();
    return this.json(mlist);
  }
}
